package m.app.action;

import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.common.model.util.QueryOrder;
import m.store.model.OrderModel;
import m.store.model.OrderRepositoryCheck;
import m.store.model.StockInfo;
import m.store.service.OrderRepositoryCheckService;
import m.system.document.DocumentMeta;
import m.system.document.DocumentMethodMeta;
import m.system.document.DocumentParamMeta;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@ActionMeta(name = "app/orderRepositoryCheck",title = "app仓库审核")
public class OrderRepositoryCheckAction extends AppAction{
    private OrderRepositoryCheck model;
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "订单商品审核列表",permission = true),
        params = {
            @DocumentParamMeta(name = "page.index",description = "分页开始位置",type = FieldType.INT),
            @DocumentParamMeta(name = "page.num",description = "分页每页数量",type = FieldType.INT),
            @DocumentParamMeta(name = "params[oid@eq]",description = "条件示例，字段和操作符中间用@，值为空不添加",type = FieldType.STRING,length = 50),
            @DocumentParamMeta(name = "params[oid#desc]",description = "排序示例，字段和操作符中间用#，值为空不生效",type = FieldType.STRING,length = 50)
        }
    )
    public JSONMessage checkList() throws Exception {
        ModelQueryList queryList=toQueryList(OrderRepositoryCheck.class,"*","goods.*");
        queryList.addCondition(QueryCondition.eq("checkUser.oid", PowerUtil.getSessionUserOid()));
        queryList.addOrder(QueryOrder.desc("orderDate"));
        return JSONMessage.success("查询成功").push("page",ModelQueryList.getModelPageInfo(queryList));
    }

    @DocumentMeta(
        method = @DocumentMethodMeta(title = "获取订单审核记录",permission = true),
        params = {
            @DocumentParamMeta(name = "orderOid",description = "订单主键",type = FieldType.STRING,length = 50,notnull = true)
        }
    )
    @RequestMeta(params = {"orderOid"})
    public JSONMessage orderCheckList(String orderOid) throws Exception {
        return JSONMessage.success("查询成功")
            .push("list",getCheckList(orderOid));
    }
    private List<OrderRepositoryCheck> getCheckList(String orderOid) throws Exception {
        return ModelQueryList.getModelList(OrderRepositoryCheck.class,new String[]{"*","goods.*","stockRecord.*"},null,
            QueryCondition.and(
                QueryCondition.eq("orderOid",orderOid),
                QueryCondition.eq("checkUser.oid", PowerUtil.getSessionUserOid()),
                QueryCondition.eq("status",OrderRepositoryCheck.Status_Check)
            )
        );
    }
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "获取订单审核失败消息",permission = true),
        params = {
            @DocumentParamMeta(name = "orderOid",description = "订单主键",type = FieldType.STRING,length = 50,notnull = true)
        }
    )
    @RequestMeta(params = {"orderOid"})
    public JSONMessage getCheckFailMessage(String orderOid) throws SQLException, MException {
        List<OrderRepositoryCheck> ls=ModelQueryList.getModelList(OrderRepositoryCheck.class,new String[]{"*","goods.name"},null,
            QueryCondition.and(
                QueryCondition.eq("orderOid",orderOid),
                QueryCondition.eq("status",OrderRepositoryCheck.Status_No)
            )
        );
        return JSONMessage.success("")
            .push("errors",
                ls.stream()
                .map(OrderRepositoryCheck::getCheckRemark).distinct()
                .collect(Collectors.toList())
            );
    }
    public OrderRepositoryCheck getModel() {
        return model;
    }

    public void setModel(OrderRepositoryCheck model) {
        this.model = model;
    }
}
